$(function(){
  var number = 0;
  var money = 0;
  var lotteryArr = [];//购彩内容
  $('#elem_jscq div').click(function(){
    $(this).siblings().removeClass('hover');
    $(this).addClass('hover');
    var index = $(this).index();
    lotteryArr = [];

    $('.common .list').eq(index).show().siblings().hide();
    number = 0;
    money = 0;
    $('#zhu').text(0);
    $('#money').text(0)
  })

  // 选择下注

  $('.bifa-m span').click(function(){
    
    $(this).toggleClass('hover');
      var lotterObj = {
        playType:$(this).data('type'),
        content:String($(this).data('content')),
        odds:$('i',this).text(),
        payPrice:2,
        times:1
      }
      if($(this).hasClass('hover')){
        lotteryArr.push(lotterObj);
        number++;
        money+=2;
      }else{
        lotteryArr.pop(lotterObj);
        number--;
        money-=2;
      }
      $('#zhu').text(number);
      $('#money').text(money);
      $('.content').data('content',lotteryArr) //记录下注信息

  })
  
  // 重置
  $('.reset').click(function(){
    number = 0;
    money = 0;
    $('#zhu').text(0);
    $('#money').text(0);
    $('.bifa-m span').removeClass('hover');
    lotteryArr = []
  })

  // 下注
  $('.action').click(function(){
    
    if(lotteryArr.length>0){
      
      layer.open({
        type: 2,
        area: ['600px', '400px'],
        fixed: false, //不固定
        maxmin: true,
        content: './action.html',
        end:function(){
          lotteryArr = [];
          number = 0;
          money = 0;
          $('#zhu').text(0);
          $('#money').text(0);
          $('.bifa-m span').removeClass('hover');
        }
      });
      
    }else{
      layer.msg('请选择你要投注的内容');
    }
    
  })

})
// 冠亚和赔率
function gyh(gyhz){
  $('.san i').text(gyhz.san);
  $('.si i').text(gyhz.si)
  $('.wu i').text(gyhz.wu)
  $('.liu i').text(gyhz.liu)
  $('.qi i').text(gyhz.qi)
  $('.ba i').text(gyhz.ba)
  $('.jiu i').text(gyhz.jiu)
  $('.shi i').text(gyhz.shi)
  $('.shiy i').text(gyhz.shiy)
  $('.shie i').text(gyhz.shie)
  $('.shis i').text(gyhz.shis)
  $('.shisi i').text(gyhz.shisi)
  $('.shiw i').text(gyhz.shiw)
  $('.shil i').text(gyhz.shil)
  $('.shiq i').text(gyhz.shiq)
  $('.shib i').text(gyhz.shib)
  $('.shij i').text(gyhz.shij)

  $('.gyd i').text(gyhz.gyd)
  $('.gyx i').text(gyhz.gyx)
  $('.gydan i').text(gyhz.gydan)
  $('.gys i').text(gyhz.gys)
  $('.gyds i').text(gyhz.gyds)
  $('.gydd i').text(gyhz.gydd)
  $('.gyxd i').text(gyhz.gyxd)
  $('.gyxs i').text(gyhz.gyxs)
}
// 1-10名大小单双龙虎赔率
function sdsm(sdsm){
  $('.dxds i').text(sdsm.dxds);
  $('.loongh i').text(sdsm.loongh)
}
// 1-10名龙虎赔率
function pmyds(pmyd){
  $('.pl i').text(pmyd);
}

function init (type){
  var obj = jsonObj;
  obj.type = type;//北京赛车
  
  $ajax(
    url+'dead_awar',
    'post',
    obj,
    function(data){

      var countDown = data.data.countDown;//倒计时
      $('#lotteryName').text(countDown.lotteryName);//中文名称
      $('#lotteryName').data('type',countDown.lotteryType);//存储彩种类型

      $('#issue').text(countDown.issue);
      if(countDown.status == -1){
        $('#status').text('已关盘')
      }else if(countDown.status == 0){
        $('#status').text('已封盘')
        
      }else if(countDown.status == 1){
        $('#status').text('离封盘')
        var oddsData = data.data.oddsData;

        // 冠亚和赔率
        gyh(oddsData.gyzh)
        // 1-10名大小单双龙虎赔率
        sdsm(oddsData.sdsm)
        // 1-10名数字赔率
        pmyds(oddsData.pmyds)
      }
      resetTime(countDown.countDown,$('#deadline'))
      
      var openResult = data.data.openResult;//上期开奖
      
      $('#prevGameNo').text(openResult.issue);
      var numberArry = openResult.number.split(',');//开奖号码
      
      var res = '';
      for(var i = 0,iL = numberArry.length;i<iL;i++){
        res += '<span class="number" style="margin-right:1px">'+numberArry[i]+'</span>'
      }
      $('#draw').append(res)

      
    },
    function(data){
      layer.msg(data.msg);
    }
  )

  // 开奖结果
  obj.rows = 10;
  obj.page = 1;
  $ajax(
    url+'lott_list',
    'post',
    obj,
    function(data){

      var list = data.data;
      var numberArr = list[1].number.split(',')
      var res = '';
      for(var i = 0,iL = list.length;i<iL;i++){
        
        res += '<ul class="ssc double_bg">'+
                  '<li>'+
                    '<div class="qi" style="margin: 6px 0 0 4px;">'+
                      ''+list[i].issue.slice(-3)+'<font class="qiZiColor">期</font>'+
                    '</div>';

        if(list[i].number !== null){
          for(var j = 0,jL=numberArr.length;j<jL;j++){
            res+='<div class="num" style="margin: 6px 0 0 1px;">'+list[i].number.split(',')[j]+'</div>'
                
          }
        }
        res += '</li>'+
              '</ul>'
            
      }
      $('#results').append(res)
    },
    function(data){
      layer.msg(data.msg);
    }
  )
  // 历史走势
  $('#tendency').click(function(){
    layer.open({
      type: 2,
      area: ['600px', '400px'],
      fixed: false, //不固定
      maxmin: true,
      content: './trend.html'
    })
  })
  // 视频
  $('#video').click(function(){
    layer.msg('敬请期待');
  })
  // 公告
  var uid = getCookie('uid');
  obj.count = 5;
  obj.uid = uid;
  
  $ajax(
    url+'message',
    'post',
    obj,
    function(data){
      
      var messageArr = data.data
      
      var mess = '';
      for(var i = 0,iL=messageArr.length;i<iL;i++){
        mess+='<li >'+
            '<a href="'+messageArr[i].url+'" class="a">'+messageArr[i].title+'</a>'+
            '<span class="date">'+messageArr[i].pubdate.split(' ')[0]+'</span>'+
          '</li>'
      }
      $('#notice').append(mess)
    },
    function(data){
      clearCookie('uid')
      layer.msg(data.msg);
    }
  )
}